xquery version "3.0";

import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";

if (request:get-method() = "POST") then
    let $number := request:get-parameter("cat-number", "")
    let $login := xmldb:login("/db/", "admin", "")
    let $data := 
        <tovar id="{util:uuid()}">
            <parameters>
                <category>{request:get-parameter("category", "")}</category>
                <model>{request:get-parameter("car-model", "")}</model>
                <price>{request:get-parameter("price", "")}</price>
            </parameters>
            <system>
                <number>{$number}</number>
                <created></created>
            </system>
            <description>{util:parse-html(request:get-parameter("description", ""))/*:HTML/*:BODY/*}</description>
        </tovar>
    let $store := xmldb:store($config:app-root || "/data/all/goods/", $data/@id || ".xml", $data)
    return 
        response:redirect-to(xs:anyURI(request:get-header("Referer")))
else response:redirect-to(xs:anyURI("/"))